﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("YUIAspNet");

YUIAspNet.AutoComplete = function(element) {
    YUIAspNet.AutoComplete.initializeBase(this, [element]);
}

YUIAspNet.AutoComplete.prototype = {
    initialize: function() {
        YUIAspNet.AutoComplete.callBaseMethod(this, 'initialize');
    },

    initializeYUIControl: function(sender, args) {
        // Use an XHRDataSource
        var ds = this.get_DataSource();

        // Define the schema of the delimited results
        //        ds.responseSchema = {
        //            recordDelim: this.get_RecordDelimiter(),
        //            fieldDelim: this.get_FieldDelimiter()
        //        };

        this._yuiControl = new YAHOO.widget.AutoComplete(this.get_TextControl(), this.get_ResultContainer(), ds);
        this._yuiControl.typeAhead = this.get_TypeAhead();
        this._yuiControl.allowBrowserAutocomplete = this.get_AllowBrowserAutocomplete();
        this._yuiControl.alwaysShowContainer = this.get_AlwaysShowContainer();
        this._yuiControl.animHoriz = this.get_AnimateHorizontally
        if (this.get_AnimationSpeed())
            this._yuiControl.animSpeed = this.get_AnimationSpeed();
        this._yuiControl.animVert = this.get_AnimateVertically();
        this._yuiControl.autoHighlight = this.get_AutoHighlight();
        this._yuiControl.forceSelection = this.get_ForceSelection();
        this._yuiControl.maxResultsDisplayed = this.get_MaxResultsDisplayed();
        this._yuiControl.minQueryLength = this.get_MinQueryLength();
        this._yuiControl.queryDelay = this.get_QueryDelay();
        this._yuiControl.supressInputUpdate = this.get_SupressInputUpdate();
        if (this.get_TypeAheadDelay())
            this._yuiControl.typeAheadDelay = this.get_TypeAheadDelay();

        this._yuiControl.useIFrame = this.get_UseIFrame();

        if (this.get_HighlightCssClass())
            this._yuiControl.highlightClassName = this.get_HighlightCssClass();
        //        if (this.get_SupressInputUpdate())
        //            this._yuiControl.prehighlightClassName = this.get_SupressInputUpdate();

        this._yuiControl.useShadow = this.get_UseShadow();
        this._yuiControl.queryQuestionMark = false;

        var ctl = this;

        if (this.get_Source().URL) { // compose query only for dynamic data!
            this._yuiControl.generateRequest = function(sQuery) {
                ret = "query=" + sQuery +
                "&max=" + ctl.get_MaxResultsDisplayed() +
                "&code=" + ctl.get_AutoCompleteCode();

                return ret;
            };
        }

        if (this._events) {
            var containerExpandHandler = this.get_events().getHandler("OnContainerExpand");
            if (containerExpandHandler)
                this._yuiControl.containerExpandEvent.subscribe(function() {
                    containerExpandHandler(ctl, { cancel: false, eventName: "OnContainerExpand" });
                });

            var dataRequestedHandler = this.get_events().getHandler("OnDataRequested");
            if (dataRequestedHandler)
                this._yuiControl.dataRequestEvent.subscribe(function() {
                    dataRequestedHandler(ctl, { cancel: false, eventName: "OnDataRequested" });
                });

            var dataRecievedHandler = this.get_events().getHandler("OnDataRecieved");
            if (dataRecievedHandler)
                this._yuiControl.dataReturnEvent.subscribe(function() {
                    dataRecievedHandler(ctl, { cancel: false, eventName: "OnDataRecieved" });
                });

            var dataErrorHandler = this.get_events().getHandler("OnDataError");
            if (dataErrorHandler)
                this._yuiControl.dataErrorEvent.subscribe(function() {
                    dataErrorHandler(ctl, { cancel: false, eventName: "OnDataError" });
                });
            else // always show the errors
                this._yuiControl.dataErrorEvent.subscribe(function(ac, query) { alert("Error getting data from " + query); });

            var containerCollapseHandler = this.get_events().getHandler("OnContainerCollapse");
            if (containerCollapseHandler)
                this._yuiControl.containerCollapseEvent.subscribe(function() {
                    containerCollapseHandler(ctl, { cancel: false, eventName: "OnContainerCollapse" });
                });

            var itemSelectedHandler = this.get_events().getHandler("OnItemSelected");
            if (itemSelectedHandler)
                this._yuiControl.itemSelectEvent.subscribe(function() {
                    itemSelectedHandler(ctl, { cancel: false, eventName: "OnItemSelected" });
                });

            var textboxLeaveHandler = this.get_events().getHandler("OnTextboxLeave");
            if (textboxLeaveHandler)
                this._yuiControl.textboxBlurEvent.subscribe(function() {
                    textboxLeaveHandler(ctl, { cancel: false, eventName: "OnTextboxLeave" });
                });

            var textboxFocusHandler = this.get_events().getHandler("OnTextboxFocus");
            if (textboxFocusHandler)
                this._yuiControl.textboxFocusEvent.subscribe(function() {
                    textboxFocusHandler(ctl, { cancel: false, eventName: "OnTextboxFocus" });
                });

            var textboxKeyPressHandler = this.get_events().getHandler("OnTextboxKeyPressed");
            if (textboxKeyPressHandler)
                this._yuiControl.textboxKeyEvent.subscribe(function(oSelf, nKeycode) {
                    textboxKeyPressHandler(ctl, { cancel: false, eventName: "OnTextboxKeyPressed", key: nKeycode });
                });

            var customFormatHandler = this.get_events().getHandler("OnCustomFormat");
            if (customFormatHandler)
                this._yuiControl.formatResult = function(oResultData, sQuery, sResultMatch) {
                    var args = { eventName: "OnCustomFormat", data: oResultData, query: sQuery, match: sResultMatch };
                    customFormatHandler(ctl, args);
                    return args.display;
                };
        }
        else
            this._yuiControl.dataErrorEvent.subscribe(function(ac, query) { alert("Error getting data from " + query); });


        this._yuiControl.dataRequestEvent.subscribe(function() { $get(ctl.get_LoadingImage()).style.display = ''; });
        this._yuiControl.dataReturnEvent.subscribe(function() { $get(ctl.get_LoadingImage()).style.display = 'none'; });
    },

    get_TextControl: function() {
        return this._textControl;
    },
    set_TextControl: function(textControl) {
        this._textControl = textControl;
    },

    get_LoadingImage: function() {
        return this._loadingImage;
    },
    set_LoadingImage: function(loadingImage) {
        this._loadingImage = loadingImage;
    },

    get_StaticData: function() {
        return this._staticData;
    },
    set_StaticData: function(staticData) {
        this._staticData = staticData;
    },

    get_ResultContainer: function() {
        return this._resultContainer;
    },
    set_ResultContainer: function(resultContainer) {
        this._resultContainer = resultContainer;
    },

    get_DataHandler: function() {
        return this._dataHandler;
    },
    set_DataHandler: function(dataHandler) {
        this._dataHandler = dataHandler;
    },

    get_DataHandler: function() {
        return this._dataHandler;
    },
    set_DataHandler: function(dataHandler) {
        this._dataHandler = dataHandler;
    },

    get_RecordDelimiter: function() {
        return this._recordDelimiter;
    },
    set_RecordDelimiter: function(recordDelimiter) {
        this._recordDelimiter = recordDelimiter;
    },

    get_FieldDelimiter: function() {
        return this._fieldDelimiter;
    },
    set_FieldDelimiter: function(fieldDelimiter) {
        this._fieldDelimiter = fieldDelimiter;
    },

    get_AllowBrowserAutocomplete: function() {
        return this._allowBrowserAutocomplete;
    },
    set_AllowBrowserAutocomplete: function(allowBrowserAutocomplete) {
        this._allowBrowserAutocomplete = allowBrowserAutocomplete;
    },

    get_AlwaysShowContainer: function() {
        return this._alwaysShowContainer;
    },
    set_AlwaysShowContainer: function(alwaysShowContainer) {
        this._alwaysShowContainer = alwaysShowContainer;
    },

    get_TypeAhead: function() {
        return this._typeAhead;
    },
    set_TypeAhead: function(typeAhead) {
        this._typeAhead = typeAhead;
    },

    get_TypeAheadDelay: function() {
        return this._typeAheadDelay;
    },
    set_TypeAheadDelay: function(typeAheadDelay) {
        this._typeAheadDelay = typeAheadDelay;
    },

    get_AnimateHorizontally: function() {
        return this._animateHorizontally;
    },
    set_AnimateHorizontally: function(animateHorizontally) {
        this._animateHorizontally = animateHorizontally;
    },

    get_AutoCompleteCode: function() {
        return this._autoCompleteCode;
    },
    set_AutoCompleteCode: function(autoCompleteCode) {
        this._autoCompleteCode = autoCompleteCode;
    },

    get_AnimateVertically: function() {
        return this._animateVertically;
    },
    set_AnimateVertically: function(animateVertically) {
        this._animateVertically = animateVertically;
    },

    get_AnimationSpeed: function() {
        return this._animationSpeed;
    },
    set_AnimationSpeed: function(animationSpeed) {
        this._animationSpeed = animationSpeed;
    },

    get_AutoHighlight: function() {
        return this._autoHighlight;
    },
    set_AutoHighlight: function(autoHighlight) {
        this._autoHighlight = autoHighlight;
    },

    get_ForceSelection: function() {
        return this._forceSelection;
    },
    set_ForceSelection: function(forceSelection) {
        this._forceSelection = forceSelection;
    },

    get_HighlightCssClass: function() {
        return this._highlightCssClass;
    },
    set_HighlightCssClass: function(highlightCssClass) {
        this._highlightCssClass = highlightCssClass;
    },

    get_PreHighlightCssClass: function() {
        return this._preHighlightCssClass;
    },
    set_PreHighlightCssClass: function(preHighlightCssClass) {
        this._preHighlightCssClass = preHighlightCssClass;
    },

    get_MaxResultsDisplayed: function() {
        return this._maxResultsDisplayed;
    },
    set_MaxResultsDisplayed: function(maxResultsDisplayed) {
        this._maxResultsDisplayed = maxResultsDisplayed;
    },

    get_MinQueryLength: function() {
        return this._minQueryLength;
    },
    set_MinQueryLength: function(minQueryLength) {
        this._minQueryLength = minQueryLength;
    },

    get_QueryDelay: function() {
        return this._queryDelay;
    },
    set_QueryDelay: function(queryDelay) {
        this._queryDelay = queryDelay;
    },

    get_SupressInputUpdate: function() {
        return this._supressInputUpdate;
    },
    set_SupressInputUpdate: function(supressInputUpdate) {
        this._supressInputUpdate = supressInputUpdate;
    },

    get_UseIFrame: function() {
        return this._useIFrame;
    },
    set_UseIFrame: function(useIFrame) {
        this._useIFrame = useIFrame;
    },

    get_UseShadow: function() {
        return this._useShadow;
    },
    set_UseShadow: function(useShadow) {
        this._useShadow = useShadow;
    },


    add_OnCustomFormat: function(handler) {
        this.get_events().addHandler("OnCustomFormat", handler);
    },
    remove_OnCustomFormat: function(handler) {
        this.get_events().removeHandler("OnCustomFormat", handler);
    },

    add_OnDataRequested: function(handler) {
        this.get_events().addHandler("OnDataRequested", handler);
    },
    remove_OnDataRequested: function(handler) {
        this.get_events().removeHandler("OnDataRequested", handler);
    },

    add_OnDataRecieved: function(handler) {
        this.get_events().addHandler("OnDataRecieved", handler);
    },
    remove_OnDataRecieved: function(handler) {
        this.get_events().removeHandler("OnDataRecieved", handler);
    },

    add_OnDataError: function(handler) {
        this.get_events().addHandler("OnDataError", handler);
    },
    remove_OnDataError: function(handler) {
        this.get_events().removeHandler("OnDataError", handler);
    },

    add_OnContainerExpand: function(handler) {
        this.get_events().addHandler("OnContainerExpand", handler);
    },
    remove_OnContainerExpand: function(handler) {
        this.get_events().removeHandler("OnContainerExpand", handler);
    },

    add_OnContainerCollapse: function(handler) {
        this.get_events().addHandler("OnContainerCollapse", handler);
    },
    remove_OnContainerCollapse: function(handler) {
        this.get_events().removeHandler("OnContainerCollapse", handler);
    },

    add_OnItemSelected: function(handler) {
        this.get_events().addHandler("OnItemSelected", handler);
    },
    remove_OnItemSelected: function(handler) {
        this.get_events().removeHandler("OnItemSelected", handler);
    },

    add_OnTextboxLeave: function(handler) {
        this.get_events().addHandler("OnTextboxLeave", handler);
    },
    remove_OnTextboxLeave: function(handler) {
        this.get_events().removeHandler("OnTextboxLeave", handler);
    },

    add_OnTextboxFocus: function(handler) {
        this.get_events().addHandler("OnTextboxFocus", handler);
    },
    remove_OnTextboxFocus: function(handler) {
        this.get_events().removeHandler("OnTextboxFocus", handler);
    },

    add_OnTextboxKeyPressed: function(handler) {
        this.get_events().addHandler("OnTextboxKeyPressed", handler);
    },
    remove_OnTextboxKeyPressed: function(handler) {
        this.get_events().removeHandler("OnTextboxKeyPressed", handler);
    }
}

YUIAspNet.AutoComplete.registerClass('YUIAspNet.AutoComplete', YUIAspNet.BaseControl);
